﻿using System;
using System.Collections.Generic;
using System.Linq;
using Project.Data;
using Project.Data.Repository;
using Project.Model.EF6.Entity;
using Project.Model.EF6.ViewModel;
using Project.Services.Tools;
namespace Project.Services
{
    public static class UserMapService
    {
        /// <summary>
        /// 处理热点地图的会员月、周排名数据
        /// </summary>
        public static int GetMapUser()
        {
            var dbRep = new DbContextRepository<EfContext>();
            try
            {
                //var listMonth = dbRep.QueryList<UserCountViewModel>("SELECT * FROM (Select Count(QR_CODE) as TOTALSCAN,OPENID From T_SGM_XLW_SALES_RECORD Where SCAN_TIME >= TO_DATE('" + DateTime.Today.AddDays(-30) + "', 'yyyy-mm-dd hh24:mi:ss') And SCAN_TIME < TO_DATE('" + DateTime.Today + "', 'yyyy-mm-dd hh24:mi:ss') Group By OPENID)WHERE ROWNUM <= 20 Order By TOTALSCAN desc");//月排名
                var listWeek = dbRep.QueryList<UserCountViewModel>("SELECT TOTALSCAN,OPENID FROM (Select Count(QR_CODE) as TOTALSCAN,OPENID,ROW_NUMBER() OVER(order by Count(QR_CODE) desc) RN From T_SGM_XLW_SALES_RECORD Where SCAN_TIME >=TO_DATE('" + DateTime.Today.AddDays(-7) + "','yyyy-mm-dd hh24:mi:ss') And SCAN_TIME <TO_DATE('" + DateTime.Today + "','yyyy-mm-dd hh24:mi:ss') Group By OPENID) WHERE RN <= 20");//周排名
                var userList = (from n in listWeek
                    let modelName = dbRep.QuerySingle<UserNameViewModel>("Select MOBILE,NICK_NAME From T_SGM_XLW_MEMBER Where OPENID='" + n.OPENID + "'")
                    select new MapUser()
                    {
                        OPENID = n.OPENID, MOBILE = modelName?.MOBILE, NICK_NAME = modelName?.NICK_NAME, COUNT = n.TOTALSCAN, TYPE = 0
                    }).ToList();
                //userList.AddRange(from n in listMonth
                //    let modelName = dbRep.QuerySingle<UserNameViewModel>("Select MOBILE,NICK_NAME From T_SGM_XLW_MEMBER Where OPENID='" + n.OPENID + "'")
                //    select new MapUser()
                //    {
                //        OPENID = n.OPENID, MOBILE = modelName?.MOBILE, NICK_NAME = modelName?.NICK_NAME, COUNT = n.TOTALSCAN, TYPE = 1
                //    });
                dbRep.Delete<MapUser>(m => m.ID > 0);//删除之前的排名数据
                dbRep.Add((IEnumerable<MapUser>)userList);
                return 0;
            }
            catch (Exception ex)
            {
                ex.ErrorHandle();
                return -1;
            }
        }


        ///// <summary>
        ///// 处理热点地图的会员月、周排名数据
        ///// </summary>
        //public static int GetMapUser()
        //{
        //    var DbRep = new DbContextRepository<EfContext>();
        //    try
        //    {
        //        var list = DbRep.QueryList<MapUserViewModel>("Select OPENID,SCAN_ARRY,SCAN_DATE From T_SGM_XLW_USER_OVERVIEW");
        //        var thirtyUserList = new List<MapUser>();//月排名
        //        var sevenUserList = new List<MapUser>();//周排名
        //        var oneDate = DateTime.Today;
        //        var thirtyDate = DateTime.Today.AddDays(-30);
        //        var sevenDate = DateTime.Today.AddDays(-7);
        //        foreach (var n in list)
        //        {
        //            var thirtyValue = 0;
        //            var sevenValue = 0;
        //            foreach (var m in n.SCAN_DATE.Split(','))
        //            {
        //                if (!string.IsNullOrEmpty(m))
        //                {
        //                    var date = DateTime.Parse(m);
        //                    if (date >= thirtyDate && date <= oneDate)
        //                    {
        //                        thirtyValue++;
        //                        if (date >= sevenDate && date <= oneDate)
        //                        {
        //                            sevenValue++;
        //                        }
        //                    }
        //                }
        //            }
        //            var thirtyCount = 0;//月扫码总数
        //            var sevenCount = 0;//周扫码总数
        //            var scanArry = n.SCAN_ARRY.Split(',');
        //            for (int i = 1; i <= thirtyValue; i++)//获取月扫码总数
        //            {
        //                if (!string.IsNullOrEmpty(scanArry[scanArry.Length - i]))
        //                {
        //                    thirtyCount += int.Parse(scanArry[scanArry.Length - i]);
        //                }
        //            }
        //            for (int i = 1; i <= sevenValue; i++)//获取周扫码总数
        //            {
        //                if (!string.IsNullOrEmpty(scanArry[scanArry.Length - i]))
        //                {
        //                    sevenCount += int.Parse(scanArry[scanArry.Length - i]);
        //                }
        //            }
        //            if (thirtyCount > 0)
        //            {
        //                //处理月排名数据
        //                if (thirtyUserList.Count < 20)
        //                {
        //                    var model = GetUser(n.OPENID, thirtyCount, 1);
        //                    thirtyUserList.Add(model);
        //                }
        //                else
        //                {
        //                    foreach (var k in thirtyUserList)
        //                    {
        //                        if (k.COUNT < thirtyCount)
        //                        {
        //                            thirtyUserList.RemoveAt(thirtyUserList.IndexOf(k));
        //                            thirtyUserList.Add(GetUser(n.OPENID, thirtyCount, 1));
        //                            break;
        //                        }
        //                    }
        //                }
        //            }
        //            //处理周排名数据
        //            if (sevenCount > 0)
        //            {
        //                if (thirtyUserList.Count < 20)
        //                {
        //                    var model = GetUser(n.OPENID, sevenCount, 0);
        //                    sevenUserList.Add(model);
        //                }
        //                else
        //                {
        //                    foreach (var k in sevenUserList)
        //                    {
        //                        if (k.COUNT < sevenCount)
        //                        {
        //                            sevenUserList.RemoveAt(sevenUserList.IndexOf(k));
        //                            sevenUserList.Add(GetUser(n.OPENID, sevenCount, 0));
        //                            break;
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        DbRep.Delete<MapUser>(m => m.ID > 0);//删除之前的排名数据
        //        DbRep.Add((IEnumerable<MapUser>)thirtyUserList);//插入月排名
        //        DbRep.Add((IEnumerable<MapUser>)sevenUserList);//插入周排名
        //        return 0;
        //    }
        //    catch (Exception ex)
        //    {
        //        ex.ErrorHandle();
        //        return -1;
        //    }
        //}

        ///// <summary>
        ///// 获取会员名称和手机号
        ///// </summary>
        ///// <param name="openId">openID</param>
        ///// <param name="count">会员周、月扫码数量</param>
        ///// <param name="type">0：周；1：月</param>
        ///// <returns></returns>
        //public static MapUser GetUser(string openId, int count, int type)
        //{
        //    var DbRep = new DbContextRepository<EfContext>();
        //    var member =
        //        DbRep.QuerySingle<UserViewModel>(
        //            "Select NICK_NAME,MOBILE From T_SGM_XLW_MEMBER Where OPENID='" + openId + "'");
        //    var model = new MapUser
        //    {
        //        OPENID = openId,
        //        MOBILE = member?.MOBILE,
        //        NICK_NAME = member?.NICK_NAME,
        //        COUNT = count,
        //        TYPE = type
        //    };
        //    return model;
        //}
    }
}
